package com.test.mooan.labama.bean_boom.product;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.test.mooan.labama.bean_boom.process_boom.ProcessInfo_boom;
import com.test.mooan.labama.constants_boom.Constant_boom;
import com.test.mooan.labama.utils_boom.LoanStatusUtil;
import com.test.mooan.labama.utils_boom.Stringutil_boom;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;

import org.json.JSONObject;

import java.io.Serializable;

public class Product implements Serializable {

    private Product(String product,String token){
        this.product = product;
        this.token = token;
    }

    public static Product gtInstance(JSONObject jsonObject){
        try {
            if (jsonObject!=null) {

                Product result = new Gson().fromJson(jsonObject.toString(),Product.class);

                if (LoanStatusUtil.isOrderPendingRepayment(result.gtStatus())) //是否在待还款-未逾期
                    result.productStatus = LoanStatusUtil.LoanStatus.Order_Pending_Repayment;
                    
                else if (LoanStatusUtil.isOrderOverdueRepayment(result.gtStatus())) //是否在待还款-已逾期
                    result.productStatus = (LoanStatusUtil.LoanStatus.Order_Overdue_Repayment);

                else if (LoanStatusUtil.isLoanReviewing(result.gtApplyStatus())) //是否有审核中的贷款
                    result.productStatus = (LoanStatusUtil.LoanStatus.Loan_Reviewing);

                else if (LoanStatusUtil.isOrderLoaning(result.gtStatus())) //是否在待放款
                    result.productStatus = (LoanStatusUtil.LoanStatus.Order_Pending);
                    
                else if (LoanStatusUtil.isLoanRejcted(result.gtApplyStatus(), result.gtStatus()))//申请是否被拒绝
                    result.productStatus = (LoanStatusUtil.LoanStatus.Loan_Rejected);

                else 
                    result.productStatus = (LoanStatusUtil.LoanStatus.No_Order);
                
                return result;
            }

        }catch (Exception e){
            if (Constant_boom.Debug) e.printStackTrace();
        }
        return new Product(Constant_boom.PRODUCT, UserInfoHelper_boom.gInstance().gToken());
    }


    @SerializedName("product")
    private String product;

    @SerializedName("amount")
    private String amount;

    @SerializedName("repayAmount")
    private String repayAmount;

    @SerializedName("token")
    private String token;

    @SerializedName("tags")
    private String[] tags;

    @SerializedName("repaymentDate")
    private String repaymentDate;

    @SerializedName("name")
    private String name;

    @SerializedName("overdueDay")
    private String overdueDay;

    @SerializedName("days")
    private String days;

    @SerializedName("currency")
    private String currency;

    @SerializedName("applyStatus")
    private String applyStatus;

    @SerializedName("status")
    private String status;

    @SerializedName("activeFlag")
    private String activeFlag;

    @SerializedName("interestRate")
    private String interestRate;

    public LoanStatusUtil.LoanStatus productStatus = LoanStatusUtil.LoanStatus.No_Order;

    private ProcessInfo_boom processInfo;

    public String gtProduct() {
        return Stringutil_boom.getSafeString(product);
    }

    public void stToken(String token) {
        this.token = token;
    }

    public String gtToken() {
        return Stringutil_boom.getSafeString(token);
    }

    public String gtName() {
        return Stringutil_boom.getSafeString(name);
    }

    public String gtAmount() {
        return Stringutil_boom.getSafeString(amount);
    }

    public int gtAmountToInt(){
        if (TextUtils.isEmpty(amount)) return 0;
        try {
            double amountD = Double.parseDouble(amount);
            return (int) amountD;
        }catch (NumberFormatException e){
            if (Constant_boom.Debug) e.printStackTrace();
        }
        return 0;
    }

    public String gtAmountText(){
        return gtAmount()+" "+gtCurrency();
    }

    public String gtCurrency() {
        return Stringutil_boom.getSafeString(currency);
    }

    public String gtDays() {
        return Stringutil_boom.getSafeString(days);
    }

    public String gtRepayAmount() {
        return Stringutil_boom.getSafeString(repayAmount);
    }

    public String gtRepayAmountText(){
        return gtRepayAmount()+" "+gtCurrency();
    }

    public String gtRepaymentDate() {
        return Stringutil_boom.getSafeString(repaymentDate);
    }

    public int gtOverdueDay() {

        if (TextUtils.isEmpty(overdueDay)) return 0;
        try {
            return Integer.parseInt(overdueDay);
        }catch (NumberFormatException e){
            if (Constant_boom.Debug) e.printStackTrace();
            return 0;
        }

    }

    public String gtStatus() {
        return Stringutil_boom.getSafeString(status);
    }

    public String gtApplyStatus() {
        return Stringutil_boom.getSafeString(applyStatus);
    }

    public boolean isActive() {
        return "YES".equalsIgnoreCase(activeFlag);
    }

    @SuppressLint("DefaultLocale")
    public String gtInterestRate() {
        if (TextUtils.isEmpty(interestRate)) return "0";
        try {
            double d = Double.parseDouble(interestRate)*100;
            return String.format("%.2f%%",d);
        }catch (NumberFormatException e){
            if (Constant_boom.Debug) e.printStackTrace();
            return Stringutil_boom.getSafeString(interestRate);
        }
    }

    public String[] gtTags() {
        if (tags == null) return new String[0];
        return tags;
    }

    public void stProcessInfo(ProcessInfo_boom processInfo) {
        this.processInfo = processInfo;
        if (processInfo!=null) this.productStatus = processInfo.gLoanStatus();
    }

    public ProcessInfo_boom gtProcessInfo() {
        return processInfo;
    }

}
